package net.sharpf2.api.interfaces;

import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.game.Interfaces;
import net.sharpf2.api.types.Content;
import net.sharpf2.api.types.Content.Actions;

/**
 * Class to handle the equipment tab.
 * @author Piotr Gajdowski
 */
public class Equipment {
    
    /**
     * Return the actual interface object for the equipment tab.
     * @return Content<Interface>
     */
    public static Content<Interface> getInterface() {
        return Interfaces.get(Constants.EQUIPMENT_TAB, 0);
    }
    
    /**
     * Returns the children of the equipment tab.
     * @param   child Child ID.
     * @return  Content<Interface>
     */
    public static Content<Interface> getChild(int child) {
        return Interfaces.get(Constants.EQUIPMENT_TAB, child);
    }
    
    /**
     * Method to click a button in the Equipment tab, for example to open the Equipment stats, price checker and items kept on death interface.
     * @param button    ID of the button, use the Constants class to indicate the ID.
     */
    public static void clickButton(int button) {
        Tabs.checkAndOpen(Tabs.Constants.TAB_EQUIPMENT);
        Content<Interface> btn = getChild(button);
        if (btn != null) {
            btn.getActions().click();
        }
    }
    
    /**
     * Method can be used to check if the character is wearing a specified item
     * @param   itemID    Item ID of the item.
     * @return  true if is wearing, otherwise false.
     */
    public static boolean isWearing(int... itemID) {
        Tabs.checkAndOpen(Tabs.Constants.TAB_EQUIPMENT);
        for (int i=0; i <= Constants.TAB_BOXES.length; i++) {
            Content<Interface> box = getChild(Constants.TAB_BOXES[i]);
            if (box != null) {
                for (int item : itemID) {
                    return item == box.getContent().getId();
                }
            }
        }
        return false;
    }
    
    /**
     * Gets the stack size of an item.
     * @param   box   Equipment box ID.
     * @return  Returns stack size.
     */
    public static int getStackSize(int box) {
        Tabs.checkAndOpen(Tabs.Constants.TAB_EQUIPMENT);
        Content<Interface> item = getChild(box);
        if (item != null) {
            return item.getContent().getStackSize();
        }
        return -1;
    }
    
    /**
     * Searches a specified item ID ine very equipment box.
     * @param   itemID    item ID you wish to search for.
     * @return  Returns the equipment box ID.
     */
    public static int searchItem(int itemID) {
        for (int i=0; i<Constants.TAB_BOXES.length; i++) {
            Content<Interface> box = getChild(Constants.TAB_BOXES[i]);
            if (box != null && box.getContent().getId() == itemID) {
                return i;
            }
        }
        return -1;
    }
    
    /**
     * Interacts with a specified equipment box ID.
     * @param boxID     Indicates which box to interact with.
     * @param action    Action to click.
     */
    public static void interactBox(int boxID, String action) {
        Content<Interface> box = getChild(Constants.TAB_BOXES[boxID]);
        if (box != null) {
            box.getActions().click(action);
        }
    }
    
    /**
     * Clicks any worn item
     * @param itemID	ID of the item
     * @param action	Action to do in item
     */
    public static void interactItem(int itemID, String action) {
    	for (int i = 0; i<Constants.TAB_BOXES.length; i++) {
            Content<Interface> item = getChild(Constants.TAB_BOXES[i]);
            if (item != null) {
    		if (item.getContent().getId() == itemID) {
                    item.getActions().click(action);
    		}
            }
    	}
    }
    
    /**
     * Class for a specified slot.
     */
    public static class Slot {

        private int slot;
        private Content<Interface> box = getChild(Constants.TAB_BOXES[slot]);

        /**
         * Gets a specified slot.
         * @param slot Slot ID.
         */
        public Slot(int slot) {
            if (Tabs.isOpen(Tabs.Constants.TAB_EQUIPMENT)) {
                this.slot = slot;
            }
        }
        
        /**
         * Returns Content methods.
         * @return Interface
         */
        public Interface getContent() {
            if (box != null) {
                return box.getContent();
            }
            return null;
        }
        
        /**
         * Returns Actions methods.
         * @return Actions
         */
        public Actions getActions() {
            if (box != null) {
                return box.getActions();
            }
            return null;
        }
        
    }
    
    public static class Constants {
        public final static int EQUIPMENT_TAB = 387;
        public final static int EQUIPMENT_STAT_IFACE = 667;
        public final static int PRICE_CHECKER_IFACE = 206;
        public final static int ITEMS_KEPT_ON_DEATH_IFACE = 17;
        public final static int TAB_HEAD = 8;
        public final static int TAB_CAPE = 11;
        public final static int TAB_NECK = 14;
        public final static int TAB_WEAPON = 17;
        public final static int TAB_BODY = 20;
        public final static int TAB_SHIELD = 23;
        public final static int TAB_LEGS = 26;
        public final static int TAB_HANDS = 29;
        public final static int TAB_FOOT = 32;
        public final static int TAB_RING = 35;
        public final static int TAB_AMMO = 38;
        public final static int TAB_AURA = 50;
        public final static int[] TAB_BOXES = {TAB_HEAD, TAB_CAPE, TAB_NECK, TAB_BODY, TAB_SHIELD, TAB_LEGS, TAB_HANDS, TAB_FOOT, TAB_RING, TAB_AMMO, TAB_AURA};
        public final static int BUTTON_EQUIPMENT = 39;
        public final static int BUTTON_PRICE_CHECKER = 42;
        public final static int BUTTON_ITEMS_KEPT_ON_DEATH = 45;
    }
    
}
